directives.directive('betweenFilter', ["$rootScope", "tools", "$timeout", "$routeParams", "tmp", function ($rootScope, tools, $timeout, $routeParams, tmp) {
    return {
        restrict: 'E',
        scope: true,
        replace: true,
        link: function (scope, element, attr) {
            var config = scope.config = scope.getCondition(attr.conditionid ? Number(attr.conditionid) : Number(attr.tmpid));
            var $ele = $(element);
            var events = {};
            var min, max;
            var cur_value = "";

            scope.ranges = [];
            var makeRangeBar = function (min_, max_) {
                $ele.find('.slider-container ').remove();

                scope.ranges.length = 0;
                scope.ranges.push({
                    shown: true
                });
                min_ = min_ || min;
                max_ = max_ || max;

                $timeout(function () {
                    $ele.find('.range-slider').val(min_ + ',' + max_).jRange({
                        from: min_,
                        to: max_,
                        step: 1,
                        scale: [min_, max_],
                        format: '%s',
                        width: $ele.width() * 0.8,
                        showLabels: true,
                        isRange: true,
                        dragEndFn: function () {
                            $rootScope.$broadcast('FILTER_EV', config.conditionId, cur_value = $('.range-slider').val());
                        }
                    });
                    $ele.find('.slider-container ').css({
                        'width': $ele.width() * 0.8 + 'px',
                        'marginLeft': $ele.width() * 0.1 + 'px',
                        'marginTop': $ele.height() * 0.35 + 'px'
                    });
                }, 50);
            }

            $ele.delegate('.slider-container', 'click', function (e) {
                e.stopPropagation();
            });

            scope.clearData = function () {
                config.column = null;
                scope.fieldTitle = '';
                scope.recover_fields();
                $ele.find('.slider-container').hide();
                scope.config.selected = scope.selected = false;
            }

            scope.clearCondition = function () {
                $rootScope.$broadcast('FILTER_CLEAR_EV', config.conditionId);
            }

            scope.refresh = function (e) {
                makeRangeBar();
                e.stopPropagation();
                $rootScope.$broadcast('FILTER_EV', config.conditionId, '');
            }

            // 获取数据
            scope.datas = [];
            scope.shown = false;
            scope.getConditionData = function (conditionId) {
                tools.get({
                    url: '/Ibm-Client/getRangeDatasByConditionId.htm',
                    data: {
                        conditionId: conditionId,
                        pageId: $routeParams.id
                    },
                    succ: function (resp) {
                        if (resp.success) {
                            cur_value = "";
                            scope.resp = resp;
                            resp.value.min = min = Number(resp.value.min);
                            resp.value.max = max = Number(resp.value.max);
                            makeRangeBar(min, max);

                            scope.config.selected = scope.selected = false;
                            $(element).find('.slider-container').show().css({
                                marginLeft: -0.5 * $(element).find('.slider-container').width() + 'px'
                            });
                            $ele.removeClass(attr.type);
                        }
                    }
                });
            }

            if (config.conditionId) {
                scope.getConditionData(config.conditionId);
            } else {
                $ele.addClass(attr.type);
            }

            // 监听取消预览事件
            events.PREVIEW_OFF_EV_OFF = $rootScope.$on('PREVIEW_OFF_EV', function () {
                /*angular.forEach(scope.datas, function (data) {
                    data.selected = false;
                });
                makeRangeBar(scope.resp);*/
            });

            events.RESIZE_EV_OFF = $rootScope.$on('RESIZE_EV', function () {
                if (scope.selected) {
                    if (!min && !max) {
                        return;
                    }

                    makeRangeBar();

                    // 重新调整到之前的数值
                    $timeout(function () {
                        if (cur_value) {
                            $ele.find('.range-slider').jRange('setValue', cur_value);
                        }
                    }, 100);
                }
            });
        },
        templateUrl: "/trend-new-BI/app/components/common/chartManager/range/rangeView.html"
    }
}]);